package com.itheima.day01.managerTest.dao;

import com.itheima.day01.managerTest.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao {
    private static ArrayList<Student> stus = new ArrayList<>();

    static{
        Student stu1 = new Student("itheima001","张三",23,"2000-11-11");
        Student stu2 = new Student("itheima002","李四",24,"2000-22-22");
        stus.add(stu1);
        stus.add(stu2);
    }

    // 添加学生
    public boolean addStudent(Student stu) {
        stus.add(stu);
        return true;
    }

    // 返回集合
    public Student[] findAllStudent() {
        Student[] students = new Student[stus.size()];
        for (int i = 0; i < students.length; i++) {
            students[i] = stus.get(i);
        }
        return students;
    }

    // 删除学生
    public void deleteStudentById(String id) {
        int index = getIndex(id);
            stus.remove(index);
            return;
    }

    // 获取id索引
    public int getIndex(String id){
        int index = -1;
        for (int i = 0; i < stus.size(); i++) {
            if(id.equals(stus.get(i).getId())){
                index = i;
                break;
            }
        }
        return index;
    }

    // 修改学生
    public void updateStudent(String updId, Student stu) {
        int index = getIndex(updId);
        stus.set(index,stu);
    }
}
